/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package czStvDpds.graphics3D.gui;

import czStvDpds.graphics3D.GraphicsIndex;
import czStvDpds.graphics3D.TextureCreator;
import czStvDpds.graphics3D.geometry.Texture;
import czStvDpds.graphics3D.geometry.TextureProperties;
import czStvDpds.input.LWJGLKeyEvent;
import czStvDpds.logging.Logging;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import javax.imageio.ImageIO;

/**
 * Napis, nejjednodussi komponenta. Nic to nedela
 *
 * @author Ondra
 */
public class LLabel extends LComponent {

    private String text;
    private int textX, textY;
    private Texture basic;
    private static final int OUTLINE = 5;

    public LLabel(String text) {
        super();
        super.model = new GUIObject(Modeler.generateLabelMesh(loc.getWidth(), loc.getHeight()));
        setText(text);
        this.createTexture();
        super.model.setTexture(basic);
    }

    private void createTexture() {
        basic = new Texture();
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File("resources/models/gui-textures/default.png"));
        } catch (IOException ex) {
            Logging.getMainLogger().log(Level.WARNING, "Failed to load texture: GUI.LLabel, because of {0}", ex.getLocalizedMessage());
        }
        TextureProperties prop = new TextureProperties();
        basic.setProperties(prop);
        prop.setType(TextureProperties.TEXTURE_2D);
        prop.setWidth(1);
        prop.setHeight(1);
        basic.setData(TextureCreator.getImageData(img));
        GraphicsIndex.getGLSynchronizer().generateTexture(basic);
    }

    /**
     * zde se definuje vykreslovani labelu.
     */
    @Override
    protected void model() {
        this.model.setPosition(loc.getX() + Modeler.pixelsToRatio(offset), loc.getY() + Modeler.pixelsToRatio(offset));
//        this.model.setText(text);
        this.model.setFont(font);
        //stanovovani pozice textu
         textX = ((Modeler.ratioToPixels(loc.getWidth()) - font.getWidth(text)) / 2);
         textY = ((Modeler.ratioToPixels(loc.getHeight()) - font.getHeight(text)) / 2);
//       this.model.setTextX(textX);
//       this.model.setTextY(textY);
        model.setText(TextPiece.cut(text, textX, textY, font));
        this.model.resize(Modeler.resizeLabelMesh(loc.getWidth() - (Modeler.pixelsToRatio(offset) * 2), loc.getHeight() - (Modeler.pixelsToRatio(offset) * 2)));
    }

    /**
     * nastavi text komponenty a podle toho i jeji velikost
     *
     * @param text
     */
    public final void setText(String text) {
        this.text = text;
//        this.model.setText(text);
        model.setText(TextPiece.cut(text, textX, textY, font));
        recalculateMinSize();
    }

    /**
     * ocividne vraci text komponenty
     *
     * @return text komponenty
     */
    public String getText() {
        return text;
    }

    /**
     * vypocitava velikost tlacitka podle textu
     */
    @Override
    protected void recalculateMinSize() {

        int textHeightPx = font.getHeight(text);
        int textWidthPx = font.getWidth(text);

        int minWidthPx = textWidthPx + (2 * OUTLINE);
        int minHeightPx = textHeightPx + (2 * OUTLINE);//?? s tou petkou

        setMinSize(new ComponentPixelSize(minHeightPx, minWidthPx));

        if (parent != null) {
            parent.childChanged(this);
        }
    }

    @Override
    protected void mouseExited() {
    }

    @Override
    protected void mouseEntered() {
    }

    @Override
    protected void mousePressed() {
    }

    @Override
    protected void mouseReleased() {
    }

    @Override
    protected void keyPressed(LWJGLKeyEvent e) {
    }

    @Override
    protected void mousePressedNotHere() {
    }
}
